package com.saikei.statstitlebar;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatEditText;

public class BarEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {

    public BarEditText(@NonNull Context context) {
        super(context);
        setOther();
        getLeftD();
        setDrawablesl();
        setRadius(30,Color.WHITE);
        setIsShowRightIcon(false);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    /**
     * 设置默认相关属性
     */
    private void setOther() {
        setImeOptions(EditorInfo.IME_ACTION_GO);
        setMaxLines(1);
        setSingleLine(true);
        setHint("请您输入......");
    }

    int radius = 12;

    protected void setRadius(int radius, int strokeColor, int color) {
        this.radius = radius;
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(radius);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            drawable.setPadding(15, 5, 15, 5);
        }
        drawable.setStroke(1, strokeColor);
        drawable.setColor(color);
        setBackground(drawable);
    }

    public void setRadius(int radius, int color) {
        setRadius(radius, color, color);
    }

    protected void setEditColorWithBar(int barColor) {
        if (TitleUtil.getRgb(barColor)) {
            setRadius(radius, Color.BLACK);
            setLeftDColor(Color.WHITE);
            setRightDColor(Color.WHITE);
        } else {
            setRadius(radius, Color.WHITE);
            setLeftDColor(Color.BLACK);
            setRightDColor(Color.BLACK);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));
                if (touchable) {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

    private boolean hasFocus;
    private Drawable rightDrawable;
    private Drawable leftDrawable;

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFocus = hasFocus;
        if (hasFocus && getText() != null) {
            boolean b = getText().toString().length() > 0;
            setIsShowRightIcon(b);
        } else {
            setIsShowRightIcon(false);
        }
    }

    /**
     * @Author:gewu
     * @Time:2022/11/300:54
     * @Description:根据焦点判断是否显示右侧叉叉
     */
    private void setIsShowRightIcon(boolean isShow) {
        if (isShow) {
            getRightD();
        }else {
            rightDrawable=null;
        }
        setDrawablesl();
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void setDrawablesl() {
        setCompoundDrawables(leftDrawable, null, rightDrawable, null);
        setCompoundDrawablePadding(20);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void getRightD() {
        rightDrawable = getResources().getDrawable(R.mipmap.chacha_icon);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        setRightDColor(rightDrawableColor);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void getLeftD() {
        leftDrawable = getResources().getDrawable(R.mipmap.sousuo_icon);
        leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
        setLeftDColor(leftDrawableColor);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (hasFocus) {
            setIsShowRightIcon(charSequence.toString().length() > 0);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    private int leftDrawableColor = Color.BLACK;
    private int rightDrawableColor = Color.BLACK;

    public void setLeftDColor(int color){
        leftDrawableColor = color;
        if (leftDrawable!=null){
            leftDrawable.setTint(color);
        }
    }

    public void setRightDColor(int color){
        rightDrawableColor = color;
        if (rightDrawable!=null){
            rightDrawable.setTint(color);
        }
    }
}
